// pages/staff/customSearch.js
const app = getApp()
var CONFIG = require("../../config/config.js");
var share = require("../../utils/share.js");
Page({

  /**
   * 页面的初始数据
   */
    data: {
        keyword:'',  //input框关键字
        data:'',
        msg:"",
        stuclose:false,
        phone:'',   //课程顾问手机号
        stuphone:'',   //学员手机号
        admin_id:'',
        errcode:0,
        identity:2,
    },

    addcourse(){
        var that = this;
        var admin_id = app.globalData.admin_id;
        wx.navigateTo({
            url: '/pages/staff/addcustomNew?type=1&phone=""&consultant_id=' + admin_id+'',
        })
    },
    
    // 点击转入我名下
    newclick(){
        var that = this;
        that.setData({
            stuclose:true
        })
    },
    // 点击取消
    chenclose(){
        var that = this;
        that.setData({
            stuclose: false
        })
    },

    // 点击确定转入
    hkclose(){
        var that = this;
        var stuphone = that.data.stuphone;
        var phone = that.data.phone;
        that.setData({
            stuclose: false
        })
        wx.showLoading({
            title: '转入中~',
            mask: true
        })
        var data={
            stuPhone: stuphone,
            conPhone: phone
        }
            
        share.wxRequest('crmapi/user/changeStu', data, function (res) {
            wx.hideLoading();
            if (Number(res.data.errcode) == 0) {
                that.stuNew(stuphone);
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2500
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 3500
                })
            }
        })
    },

    //  输入框改变时
    chackinput:function(){
        var that = this;
        that.setData({
            data: ''
        })
    },



    // 点击查询
    formSubmit:function(e){
        // console.log(e);
        var that = this;
        that.setData({
            data:''
        })
        var phone = e.detail.value.phone;
        var data = phone.replace(/\s+/g, "");
        // console.log(data);
        // console.log(phone);
        if(phone == ''){
            wx.showToast({
                title: '请输入要查询的客户的手机号',
                icon: 'none',
                duration: 1500
            })
        } else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(data))){
            wx.showToast({
                title: '请输入正确的手机号以便您的查询',
                icon: 'none',
                duration: 1500
            })
        }else{
            that.stuNew(data);
        }
    },
    stuNew(data){
        var that = this;
        wx.showLoading({
            title: '正在查询中~',
            mask: true
        })
        // 获取课程顾问id
        var admin_id = app.globalData.admin_id;
        that.setData({
            admin_id: admin_id,
            data:'',
        })
        share.wxRequest('crmapi/user/queryStu', { phone: data}, function (res) {
            // console.log(res);
            wx.setStorageSync('phoneseacher', data);
            wx.hideLoading();
            if (Number(res.data.errcode) == 0) {
                that.setData({
                    data: res.data.data,
                    stuphone: data,
                    errcode: 2
                })
            } else {
                that.setData({
                    errcode: 1
                })
            }

        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        var admin_id = app.globalData.admin_id;
        that.setData({
            admin_id: admin_id,
            identity: app.globalData.identity,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
       
        var phone = wx.getStorageSync('tel');
        that.setData({
            phone: phone
        })
        if (wx.getStorageSync('phoneseacher')){
            var phoneseacher = wx.getStorageSync('phoneseacher');
            that.stuNew(phoneseacher);
            
            that.setData({
                stuphone: phoneseacher
            })
        }
    },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})